package com.xnsy.jishexiangmu.controller;

import com.xnsy.jishexiangmu.pojo.Article;
import com.xnsy.jishexiangmu.service.article.ArticleService;
import com.xnsy.jishexiangmu.uitl.Result;
import com.xnsy.jishexiangmu.uitl.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")

public class ArticleController {
    @Autowired
    private ArticleService articleService;
    // 获取当前用户文章
    @GetMapping("/get")
    public Result getArticle(){
        List<Article> article = articleService.get();
        return Result.success(article);
    }
    // 获取所有文章
    @GetMapping("/getall")
    public Result getAllArticle(){
        List<Article> article = articleService.getAll();
        return Result.success(article);
    }

    // 添加新文章
    @PostMapping("/add")
    public Result addArticle(@RequestBody Article article){
        articleService.add(article);
        return Result.success("添加成功");
    }
    // 删除文章
    @PutMapping("/delete")
    public Result deleteArticle(@RequestBody Map<String, Integer> aid){
        articleService.deleteArticle(aid.get("aid"));
        return Result.success("删除成功");
    }

    // 发布文章
    @PutMapping("/fabu")
    public Result fabuArticle(@RequestBody Map<String, Integer> aid){
        articleService.fabuArticle(aid.get("aid"));
        return Result.success("发布成功");
    }
    // 修改文章
    @PutMapping("/updata")
    public Result updataArticle (@RequestBody Article article){
        articleService.updataArticle(article);
        return Result.success("修改成功");
    }
    @GetMapping("/getarticle")
    public Result getarticle(@RequestParam Integer id){
        Article article = articleService.getarticle(id);
        return Result.success(article);
    }
}
